#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - 退款通知处理功能使用示例
"""

import os
import sys
import json

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))

from examples.base_example import BaseExample
from jlpay.model.trans.refunds.refunds_notify import RefundsNotifyMessage


class RefundsNotifyExample(BaseExample):
    """退款通知处理示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.refunds.refunds_notify")
    
    def process_refunds_notify(self, notify_data):
        """
        处理退款通知
        
        Args:
            notify_data: 通知数据字典
            
        Returns:
            处理结果字典
        """
        self.logger.info("开始处理退款通知")
        
        try:
            # 创建退款通知消息对象
            notify_message = RefundsNotifyMessage(notify_data)
            
            self.logger.info("收到退款通知")
            self.logger.info(f"通知报文：{notify_message.to_dict()}")
            
            # 根据状态处理不同的业务逻辑
            if notify_message.status == "1":
                self.logger.info("退款状态：待确认")
                # 处理待确认状态
                pass
            elif notify_message.status == "2":
                self.logger.info("退款状态：成功")
                # 处理成功状态
                # 可以更新本地订单状态、发送成功通知等
                pass
            elif notify_message.status == "3":
                self.logger.info("退款状态：失败")
                # 处理失败状态
                # 可以记录失败原因、重试等
                pass
            else:
                self.logger.warning(f"未知状态：{notify_message.status}")
            
            # 返回成功响应
            result = {"code": "00", "message": "处理成功"}
            self.logger.info(f"处理结果：{result}")
            return result
            
        except Exception as e:
            self.logger.error(f"处理退款通知失败：{e}")
            # 返回失败响应
            result = {"code": "99", "message": "处理失败"}
            self.logger.error(f"处理结果：{result}")
            return result
    
    def build_mock_notify_data(self):
        """
        构建模拟的退款通知数据
        
        Returns:
            模拟的通知数据字典
        """
        return {
            "status": "2",  # 成功
            "summary": "退款成功",
            "mch_id": "849584358120018",
            "transaction_id": "451147688260420350771289",
            "out_trade_no": "REFUNDS_TEST_001",
            "chn_transaction_id": "CHN123456789",
            "ori_out_trade_no": "TEST_ORDER_001",
            "ori_transaction_id": "80011557201909171853011568",
            "total_fee": "100",
            "trans_time": "20241230143000",
            "success_time": "20241230143100"
        }
    
    def refunds_notify_demo(self):
        """退款通知处理示例"""
        self.logger.info("开始执行退款通知处理示例")
        
        # 构建模拟的通知数据
        mock_notify_data = self.build_mock_notify_data()
        
        # 处理退款通知
        result = self.process_refunds_notify(mock_notify_data)
        
        self.logger.info(f"最终处理结果：{json.dumps(result, ensure_ascii=False, indent=2)}")
        self.logger.info("退款通知处理示例执行完毕")


def main():
    """主函数"""
    example = RefundsNotifyExample()
    example.refunds_notify_demo()


if __name__ == "__main__":
    main() 